<?php


namespace app\admin\controller;


use app\admin\validate\AdForm;
use think\Db;

class Ad extends Common
{

    public function adlist(){
        $type = intval(input('param.type_id',1));
        $list = Db::name('ad')
            ->where('type_id',$type)
            ->order('id','desc')->field('id,title,url,link,is_show,create_time')->paginate(10,[]);
        return view('',[
            'list'=>$list,
            'type_list'=>Db::name('ad_type')->cache(true)->field('id,type_name')->select()
        ]);

    }
    public function setShow($id,$is_show){
       return checkRes(Db::name('ad')->where('id',$id)->update(['is_show'=>$is_show]),'修改');
    }
    public function rm_ad($id){
         return checkRes(Db::name('ad')->where('id',$id)->delete(),'删除');
    }
    public function add_ad(){
        if(Request()->isPost()) {
            $AuthForm = new AdForm();
            $data = input('post.');
            $data['is_show'] = isset($data['is_show']) && $data['is_show'] == 'on' ? 1 : 0;
            if (!$AuthForm->check($data)) {
                return package(20001, $AuthForm->getError());
            }
            return $AuthForm->add($data);
        }else{

            return view('',['type'=>'add_ad','type_list'=>Db::name('ad_type')->cache(true)->field('id,type_name')->select()]);
        }
    }

    public function editAd()
    {
        if(Request()->isPost()) {
            $AuthForm = new AdForm();
            $data = input('post.');
            $data['is_show'] = isset($data['is_show']) && $data['is_show'] == 'on' ? 1 : 0;
            if (!$AuthForm->check($data)) {
                return package(20001, $AuthForm->getError());
            }
            return $AuthForm->edit($data);
        }else{
            $id = input('param.id','0','int');
            return view('add_ad',['type'=>'editAd',

                'type_list'=>Db::name('ad_type')->cache(true)->field('id,type_name')->select(),
                'info'=>Db::name('ad')->where('id',$id)->find()
            ]);
        }


    }
}